# -*- coding: utf-8 -*-
import tkinter as tk
from tkinter import ttk
import acceptancedb
from datetime import datetime
import pymssql

class AcceptanceInputFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.AcceptanceInput_window = tk.Toplevel(self.master)
        self.AcceptanceInput_window.title('领取信息录入')
        self.AcceptanceInput_window.geometry('%dx%d' % (470, 300))
        self.page = tk.Frame(self.AcceptanceInput_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.aid = tk.StringVar()
        self.sno = tk.StringVar()
        self.number = tk.StringVar()
        self.time = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='职工号: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.aid).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='学号: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sno).grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='领取数量: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.number).grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='领取时间: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.time).grid(row=5, column=1, sticky=tk.E)
        tk.Button(self.page, text='录入领取信息', command=self.record_AcceptanceInput).grid(row=6, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=7, column=1, sticky=tk.E, pady=10)

    def record_AcceptanceInput(self):
        ISBN = self.ISBN.get()
        aid = self.aid.get()
        sno = self.sno.get()
        number = self.number.get()
        time = self.time.get()

        if not ISBN or not aid or not sno or not time:
            self.status.set('录入失败，ISBN、管理员号、学号、领取时间均不能为空')
            return

        if not acceptancedb.isbn_exists(ISBN):
            self.status.set('录入失败，ISBN不存在')
            return

        if not acceptancedb.aid_exists(aid):
            self.status.set('录入失败，管理员号不存在')
            return

        if not acceptancedb.sno_exists(sno):
            self.status.set('录入失败，学号不存在')
            return

        try:
            order_time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
        except ValueError:
            self.status.set('录入失败，时间格式不正确')
            return

        if acceptancedb.acceptance_exists(ISBN, aid, sno, time):
            self.status.set('录入失败，信息已存在')
            return

        try:
            acceptancedb.insert_acceptance(ISBN, aid, sno, number, order_time)
            self.status.set('领取信息录入成功')
        except pymssql.OperationalError as e:
            self.status.set('录入失败，库存数量不足')



class AcceptanceQueryFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.AcceptanceQuery_window = tk.Toplevel(self.master)
        self.AcceptanceQuery_window.title('领取信息查询')
        self.AcceptanceQuery_window.geometry('%dx%d' % (470, 300))
        self.page = tk.Frame(self.AcceptanceQuery_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.aid = tk.StringVar()
        self.sno = tk.StringVar()
        self.number = tk.StringVar()
        self.time = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='职工号: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.aid).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='学号: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sno).grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='领取数量: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.number, state='readonly').grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='领取时间: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.time).grid(row=5, column=1, sticky=tk.E)
        tk.Button(self.page, text='查询领取信息', command=self.query_AcceptanceInfo).grid(row=6, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=7, column=1, sticky=tk.E, pady=10)

    def query_AcceptanceInfo(self):
        ISBN = self.ISBN.get()
        aid = self.aid.get()
        sno = self.sno.get()
        time = self.time.get()
        if not ISBN or not aid or not sno:
            self.status.set('查询失败，ISBN、职工号、学号均不能为空')
            self.number.set('')
            self.time.set('')
            return

        result = acceptancedb.query_by_ISBN_aid_sno_time(ISBN, aid, sno,time)
        if result:
            self.number.set(result['number'])
            self.time.set(result['time'])
            self.status.set('查询成功')
        else:
            self.number.set('')
            self.time.set('')
            self.status.set('没有找到对应的领取信息')


class AcceptanceChangeFrame(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.AcceptanceChange_window = tk.Toplevel(self.master)
        self.AcceptanceChange_window.title('领取信息修改')
        self.AcceptanceChange_window.geometry('%dx%d' % (470, 300))
        self.page = tk.Frame(self.AcceptanceChange_window)
        self.page.pack()
        self.ISBN = tk.StringVar()
        self.aid = tk.StringVar()
        self.sno = tk.StringVar()
        self.number = tk.StringVar()
        self.time = tk.StringVar()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Label(self.page, text='ISBN: ').grid(row=1, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.ISBN).grid(row=1, column=1, sticky=tk.E)
        tk.Label(self.page, text='职工号: ').grid(row=2, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.aid).grid(row=2, column=1, sticky=tk.E)
        tk.Label(self.page, text='学号: ').grid(row=3, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.sno).grid(row=3, column=1, sticky=tk.E)
        tk.Label(self.page, text='领取数量: ').grid(row=4, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.number).grid(row=4, column=1, sticky=tk.E)
        tk.Label(self.page, text='领取时间: ').grid(row=5, sticky=tk.W, pady=10)
        tk.Entry(self.page, textvariable=self.time).grid(row=5, column=1, sticky=tk.E)
        tk.Button(self.page, text='修改领取信息', command=self.change_AcceptanceInfo).grid(row=6, column=1, sticky=tk.E, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=7, column=1, stick=tk.E, pady=10)

    def change_AcceptanceInfo(self):
        ISBN = self.ISBN.get()
        aid = self.aid.get()
        sno = self.sno.get()
        time = self.time.get()
        data = {
            'aid': aid,
            'sno': sno,
            'number': self.number.get(),
            'time': time,
        }

        if not ISBN or not aid or not sno:
            self.status.set('修改失败，ISBN、职工号、学号均不能为空')
            return

        try:
            result = acceptancedb.query_by_ISBN_aid_sno_time(ISBN, aid, sno,time)
            if result:
                acceptancedb.update_by_ISBN_aid_sno_time(ISBN, aid, sno,time, data)
                self.status.set('修改成功')
            else:
                self.status.set('没有找到对应的领取信息')

        except pymssql.exceptions.OperationalError as e:
            self.status.set('修改失败，库存数量不足')



class AcceptanceDeleteFrame(tk.Frame):
    def __init__(self, master, tree_view, item, ISBN, aid, sno, time):
        super().__init__(master)
        self.master = master
        self.tree_view = tree_view
        self.item = item
        self.ISBN = ISBN
        self.aid = aid
        self.sno = sno
        self.time = time
        self.AcceptanceDelete_window = tk.Toplevel(self.master)
        self.AcceptanceDelete_window.title('是否删除')
        self.AcceptanceDelete_window.geometry('%dx%d' % (220, 50))
        self.page = tk.Frame(self.AcceptanceDelete_window)
        self.page.pack()
        self.status = tk.StringVar()
        self.create_page()

    def create_page(self):
        tk.Button(self.page, text='是', command=self.confirm_delete).grid(row=4, column=0, sticky=tk.E, pady=10)
        tk.Button(self.page, text='否', command=self.cancel_delete).grid(row=4, column=2, sticky=tk.W, pady=10)
        tk.Label(self.page, textvariable=self.status).grid(row=5, column=1, sticky=tk.E, pady=10)

    def confirm_delete(self):
        acceptancedb.delete_by_ISBN_aid_sno_time(self.ISBN, self.aid, self.sno, self.time)
        self.tree_view.delete(self.item)

        # 关闭窗口
        self.AcceptanceDelete_window.destroy()

    def cancel_delete(self):
        self.AcceptanceDelete_window.destroy()